import java.util.Scanner;
public class NodeA {
    public class Friend {     //定义内部类Friend，用于表示链表中的一个节点
        public Friend next;
        private  String Name;
        private String Tel;
        private String Email;
        private String BornAdd;
        public Friend(){}    //空的构造方法，用于初始化节点
        public Friend(String Name,String Tel,String Email,String BornAdd){
            this.Name=Name;
            this.Tel=Tel;
            this.Email=Email;
            this.BornAdd=BornAdd;
        }
        public String getName(){
            return Name;
        }
        public void setName(String Name){
            this.Name=Name;
        }
        public void setTel(String Tel){
            this.Tel=Tel;
        }
        public String getTel(){   //获取联系人的电话号码
            return Tel;
        }
        public void setEmail(String Email){
            this.Email=Email;}
        public String getEmail(){
            return Email;}
        public void setBornAdd(String BornAdd){
            this.BornAdd=BornAdd;}
        public String getBornAdd(){
            return BornAdd;}
        public void message(String Name,String Tel,String Email,String BornAdd){
            this.Name=Name;
            this.Tel=Tel;
            this.Email=Email;
            this.BornAdd=BornAdd;}
        public String toString()
        { return this.Name+"\t"+this.Tel+"\t"+this.Email+"\t"+this.BornAdd;
        }
    }
    private int size;
    private Friend head;   //定义链表头节点
    {
        this.size=0;
        this.head=null;
    }
    public void insertHead(Friend s) {    //在链表头插入一个新的Friend节点
        if (this.size == 0)
        {this.head=s;
        }
        else{s.next=this.head;
            this.head=s;
        }
        this.size++;
    }
    public boolean delete(String Name){   //根据姓名删除链表中的Friend节点
        Friend front=this.head;
        if(Name.equals(front.getName())) {
            this.head = front.next;
            this.size--;
            return true;
        }
        Friend current = this.head;
        while (current.next != null) {
            if (current.next.getName().equals(Name)) {
                current.next = current.next.next;
                this.size--;
                return true;
            }
            current = current.next;
        }
        return false;
        //请自行编写代码完成上述功能
    }
    public Friend select(String Name){   //根据姓名查找链表中的Friend节点
        Friend front=this.head;
        if(Name.equals(front.getName())){
            Friend s=new Friend(Name,front.getTel(),front.getEmail(),front.getBornAdd());
            return s;
        }
        while (front.next!=null){
            front=front.next;
            if(Name.equals(front.getName())){
                Friend s=new Friend(Name,front.getTel(),front.getEmail(),front.getBornAdd());
                return s;
            }
        }
        return null;
    }
    public void display(){   //输出链表中的所有Friend节点
        System.out.println("姓名\t手机号\t邮箱地址\t家庭住址");
        Friend front=this.head;
        while (front!=null&&front.getName()!=null){
            System.out.println(front.toString()) ;
            front=front.next;
        }
        if(this.head==null||this.head.getName()==null){
            System.out.println("无相关信息");
        }
    }
    public static void main(String[] args) {
        NodeA sm = new NodeA();
        Scanner in = new Scanner(System.in);
        boolean open = true;
        while (open) {
            printMenu();
            System.out.println("请输入菜单序号进行操作");
            int menu = -1;
            try {
                menu = in.nextInt();
            } catch (Exception e) {
                in = new Scanner(System.in);
            }
            switch (menu) {
                case 0: {
                    System.out.println("创建成功");
                }
                break;
                case 1: {
                    System.out.println("请输入姓名");
                    String Name1 = in.next();
                    System.out.println("手机号，邮箱地址，宿舍地址（空格结束，回车确认插入）");
                    try {
                        String Tel = in.next();
                        String Email = in.next();
                        String BornAdd = in.next();
                        Friend news = sm.new Friend(Name1, Tel, Email, BornAdd);
                        sm.insertHead(news);
                    } catch (Exception e) {
                        System.out.println("输入信息有误");
                        in = new Scanner(System.in);
                    }
                    break;
                }
                case 2: {
                    System.out.println("请输入要查询联系人的姓名");
                    String Name2 = in.next();
                    Friend selectS = sm.select(Name2);
                    System.out.println("姓名\t手机号\t邮箱地址\t宿舍地址");
                    if (selectS == null) {
                        System.out.println("无相关信息");
                    } else {
                        System.out.println(selectS.toString());
                    }
                }
                break;
                case 3: {
                    System.out.println("请输入要删除联系人的姓名");
                    String Name3 = in.next();
                    if (sm.delete(Name3)) {
                        System.out.println("删除成功");
                    } else {
                        System.out.println("删除失败");
                    }
                    sm.display();
                }
                break;
                case 4: {
                    sm.display();
                }
                break;
                case 5: {
                    System.out.println("系统已退出");
                    System.exit(0);
                    break;
                }
                default:
                    System.out.println("输入错误");
            }
        }
        in.close();
    }
    public static  void printMenu(){
        System.out.println("24软件1/2班宿舍管理系统");
        System.out.println("*****************");
        System.out.println("* 0----创建链表   *");
        System.out.println("* 1----插入联系人 *");
        System.out.println("* 2----查找联系人 *");
        System.out.println("* 3----删除联系人 *");
        System.out.println("* 4----输出通讯录 *");
        System.out.println("* 5----退出系统   *");
        System.out.println("*****************");
        System.out.println("*本程序由P20240311018 李广锋设计并拥有全部代码版权*");//写上你的名字
    }
}

